package cn.chengpengper.seckill.order.domain.service.impl;

import cn.chengpengper.seckill.common.code.HttpCode;
import cn.chengpengper.seckill.common.constants.SeckillConstants;
import cn.chengpengper.seckill.common.exception.SeckillException;
import cn.chengpengper.seckill.common.model.enums.SeckillOrderStatus;
import cn.chengpengper.seckill.mq.MessageSenderService;
import cn.chengpengper.seckill.order.domain.event.SeckillOrderEvent;
import cn.chengpengper.seckill.order.domain.model.entity.SeckillOrder;
import cn.chengpengper.seckill.order.domain.repository.SeckillOrderRepository;
import cn.chengpengper.seckill.order.domain.service.SeckillOrderDomainService;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author chengpeng.hu
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SeckillOrderDomainServiceImpl implements SeckillOrderDomainService {

    private final SeckillOrderRepository seckillOrderRepository;

    private final MessageSenderService messageSenderService;

    @Value("${message.mq.type}")
    private String eventType;

    @Override
    public boolean saveSeckillOrder(SeckillOrder seckillOrder) {
        log.info("SeckillOrderDomainService|下单|{}", JSON.toJSONString(seckillOrder));
        if (Objects.isNull(seckillOrder)) {
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        seckillOrder.setStatus(SeckillOrderStatus.CREATED.getCode());
        boolean saveSuccess = seckillOrderRepository.saveSeckillOrder(seckillOrder);
        if (saveSuccess) {
            log.info("SeckillOrderDomainService|创建订单成功|{}", JSON.toJSONString(seckillOrder));
            SeckillOrderEvent seckillOrderEvent = new SeckillOrderEvent(seckillOrder.getId(), SeckillOrderStatus.CREATED.getCode(), getTopicEvent());
            messageSenderService.send(seckillOrderEvent);
        }
        return saveSuccess;

    }

    @Override
    public List<SeckillOrder> getSeckillOrderByUserId(Long userId) {
        if (Objects.isNull(userId)) {
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillOrderRepository.getSeckillOrderByUserId(userId);
    }

    @Override
    public List<SeckillOrder> getSeckillOrderByGoodsId(Long goodsId) {
        if (Objects.isNull(goodsId)) {
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillOrderRepository.getSeckillOrderByGoodsId(goodsId);
    }

    @Override
    public void deleteOrderShardingUserId(Long orderId, Long userId) {
        if (orderId == null || userId == null){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        seckillOrderRepository.deleteOrderShardingUserId(orderId, userId);
    }

    @Override
    public void deleteOrderShardingGoodsId(Long orderId, Long goodsId) {
        if (orderId == null || goodsId == null){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        seckillOrderRepository.deleteOrderShardingGoodsId(orderId, goodsId);
    }

    /**
     * 获取主题事件
     */
    private String getTopicEvent() {
        return SeckillConstants.EVENT_PUBLISH_TYPE_ROCKETMQ.equals(eventType) ? SeckillConstants.TOPIC_EVENT_ROCKETMQ_ORDER : SeckillConstants.TOPIC_EVENT_COLA;
    }

}
